package com.titaniume.java1;

import java.util.Objects;

/**
 *@program: JavaSenior
 *@description:
 *@author: renfei.huang
 *@create: 2021-10-11 17:17
 */
public class User implements  Comparable {
    private String name;
    private int age;

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }


    @Override
    public boolean equals(Object o) {
        System.out.println("User equals.......");
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age &&
                Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }



    //按照姓名从小到大排序 按照年龄从小到大排序
    @Override
    public int compareTo(Object o) {

        if(o instanceof  User){
            User u =(User)o;

            //只考虑姓名
//            return  - this.name.compareTo(u.name);
            int compare = - this.name.compareTo(u.name);
            if(compare!=0){
                return compare;
            }else {
                return Integer.compare(this.age,u.age);
            }

        }else{
            throw  new RuntimeException("输入类型不匹配");
        }
    }
}
